package com.saifan.wyy_ov.ui.communitycenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;

import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.ui.user.LoginActivity;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.utils.ToastUtil;

import org.json.JSONObject;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-20 17:29:27
 * @descrption
 */

public class ChangePWDActivity extends BaseActivity {
    private Toolbar toolbar;
    private EditText oldPwd;
    private EditText newPwd1;
    private EditText newPwd2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();

    }

    public void setContentView() {
        setContentView(R.layout.activity_change_pwd);
        this.newPwd2 = (EditText) findViewById(R.id.newPwd2);
        this.newPwd1 = (EditText) findViewById(R.id.newPwd1);
        this.oldPwd = (EditText) findViewById(R.id.oldPwd);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public void change(View view) {
        if (oldPwd.getText().toString().trim().length()<6){
            ToastUtil.showToast(this,"旧密码不能为空且长度不能少于6个字符");
            return;
        }
        if (newPwd1.getText().toString().trim().length()<6){
            ToastUtil.showToast(this,"密码不能为空且长度不能少于6个字符");
            return;

        }
        if (newPwd2.getText().toString().trim().length()<6){
            ToastUtil.showToast(this,"确认密码不能为空且长度不能少于6个字符");
            return;
        }
        if (oldPwd.getText().toString().trim().equals(newPwd1.getText().toString().trim())){
            ToastUtil.showToast(this,"新密码不能和旧密码一样");
            return;
        }
        if (!newPwd2.getText().toString().trim().equals(newPwd1.getText().toString().trim())){
            ToastUtil.showToast(this,"新密码与确认密码不同，请重新确认");
            return;
        }

        try {
            GetData getData =new GetData();
            JSONObject jsonObject = new JSONObject();
            //  参数{"UserName":"","OldPassword":"","Password":""}
            jsonObject.put("UserName", getRoomInfoBean().getWYKHDA_KHYHM());
            jsonObject.put("OldPassword", oldPwd.getText().toString().trim());
            jsonObject.put("Password", newPwd1.getText().toString().trim());
            String method = Api.Customer.CHANGE_PASSWORD;
            if (getRoomInfoBean().getUserType() == 2){
                method = Api.Customer.CHANGEPASSWORDTOURIST;
            }

            getData.getDataJson(this, method, jsonObject, getString(R.string.wait), new ResponseData() {
                @Override
                public void onSuccess(String json) {
                    Intent intent = new Intent(ChangePWDActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);


                }

                @Override
                public void onFailed(String str) {

                }
            });
        }catch (Exception e){

        }


    }


}
